<?
	/*
	// File:	backup.php
	// Purpose:	backups
	// Author:	Peter Drake <peterdrake@241computers.com>
	*/

// BackupParms = databases,files,email,local,remote,domainlist,ftpserver,ftpuser,ftppass,ftpdir
mysql_connect($cfg['dbhost'], $cfg['dbuser'], $cfg['dbpass']);
mysql_select_db($cfg['dbname']);
// If we have some domains then email / file backup
if (is_array($domains)) {
	for ($i=0; $i<sizeOf($domains); $i++) {
		if ($domains[$i] != "") {
			
			// Log backup started
			webcp_log(3,0,'backup.php',"Backup: Started for ".$domains[$i],0,$echo);
			
			$filename = $domains[$i]."-".time().".tar";
			$compressedFilename = $filename.".gz";
			
			// File backup
			if ($backupParms[1] == "on") {
				chdir($cfg['webdir'].'/'.$domains[$i]);
				exec($cfg['prog']['tar'].' -chf /tmp/'.$filename.' `ls | '.$cfg['prog']['grep'].' -v _backups` >/dev/null');
			} else {
				exec('touch /tmp/'.$filename);
			}
			
			// E-mail backup
			if ($backupParms[2] == "on") {
				$SQL = "SELECT id FROM domains WHERE domain = '" . $domains[$i] . "'";
				$dres = mysql_query($SQL);
				if ($drow = mysql_fetch_array($dres)) {
					$SQL = "SELECT username FROM users WHERE id = ".$drow["id"];
					$ures = mysql_query($SQL);
		
					for($j=0; $row=mysql_fetch_array($ures); $j++) {
						// get the real username if necessary
						$data['id'] = $drow['id'];
						$data['username'] = $row['username'];
						call_hook('user:get:username', $data);
						exec($cfg['prog']['tar'].' -rhf /tmp/'.$filename.' /var/mail/'.$data["username"].' >/dev/null');
					}
				}
			} else {
				$SQL = "SELECT id FROM domains WHERE domain = '" . $domains[$i] . "'";
				$dres = mysql_query($SQL);
				$drow = mysql_fetch_array($dres);
			}			
			exec($cfg['prog']['gzip'].' /tmp/'.$filename);
			
			// Perform local backup
			if ($backupParms[3] == "on") {
				if (copy('/tmp/'.$compressedFilename, $cfg['webdir'].'/'.$domains[$i].'/_backups/'.$compressedFilename)) {
					chgrp($cfg['webdir'].'/'.$domains[$i].'/_backups/'.$compressedFilename, 'g'.$drow["id"]);
					chown($cfg['webdir'].'/'.$domains[$i].'/_backups/'.$compressedFilename, $cfg['httpd_user']);
					webcp_log(3,0,'backup.php',"Backup: Local Successful for ".$domains[$i],0,$echo);
				} else {
					webcp_log(3,0,'backup.php',"Backup: Local Failed for ".$domains[$i],0,$echo);
				}
			}

			// Perform remote backup
			if ($backupParms[4] == "on") {
				// Connect to FTP server
				$conn_id = ftp_connect($backupParms[6]);
				if (!$conn_id) {
					webcp_log(3,0,'backup.php',"Note: Connection failed to ".$backupParms[6],0,$echo);
				}
				// Login to FTP server
				$login_result = ftp_login($conn_id, $backupParms[7], $backupParms[8]);
				// Change FTP current directory if necessary
				if ($backupParms[9] != "") {
					$dir_result = ftp_chdir($conn_id, $backupParms[9]);
				}
				// Upload file
				if (ftp_put($conn_id, $compressedFilename, '/tmp/'.$compressedFilename, FTP_BINARY)) {
					webcp_log(3,0,'backup.php',"Backup: Remote Successful for ".$domains[$i],0,$echo);
				} else {
					webcp_log(3,0,'backup.php',"Backup: Remote Failed for ".$domains[$i],0,$echo);
				}
				ftp_close($conn_id);
			}
			
			// Remove tmp file
			unlink('/tmp/'.$compressedFilename);
			// Log successful backup
			webcp_log(3,0,'backup.php',"Backup: Completed for ".$domains[$i],0,$echo);
		}
	}
}
// Database backup
if ($backupParms[0] == "on") {
	
	// Log backup started
	webcp_log(3,0,'backup.php',"Backup: Started Databases",0,$echo);
	
	$filename = "Databases_Backup_".time().".tar.gz";
	$mysql_filename = 'mysql_databases.sql';
	$pgsql_filename = 'pgsql_databases.sql';
	
	if ($cfg['user_mysql'] == true) {
		$checkDB = $cfg['prog']['mysqlcheck'].' --analyze --optimize --check-only-changed --auto-repair --all-databases -u'.$cfg['dbadminuser'].' -p'.$cfg['dbadminpass'];
		exec($checkDB);
		$dumpDB = $cfg['prog']['mysqldump'].' -Ar/tmp/'.$mysql_filename.' -u'.$cfg['dbadminuser'].' -p'.$cfg['dbadminpass'];
		exec($dumpDB);
	}
	if ($cfg['user_pgsql'] == true) {
		$dumpDB = 'export PGPASSWORD='.$cfg['pgsql_pass'].';'.$cfg['prog']['pgsqldumpall'].' > /tmp/'.$pgsql_filename.' -U'.$cfg['pgsql_user'];
		exec($dumpDB);
	}
	
	if (($cfg['user_pgsql'] == true) && ($cfg['user_mysql'] == true)) {
		$backup_files = '/tmp/'.$mysql_filename.' /tmp/'.$pgsql_filename;
	}	elseif ($cfg['user_mysql'] == true) {
		$backup_files = '/tmp/'.$mysql_filename;
	}	elseif ($cfg['user_pgsql'] == true) {
		$backup_files = '/tmp/'.$pgsql_filename;
	}

	exec($cfg['prog']['tar'].' -chzf /tmp/'.$filename.' '.$backup_files);

	if ($backupParms[4] == "on") {
		$conn_id = ftp_connect($backupParms[6]);
		if (!$conn_id) {
			webcp_log(3,0,'backup.php',"Note: Connection failed to ".$backupParms[6],0,$echo);
		}
		$login_result = ftp_login($conn_id, $backupParms[7], $backupParms[8]);
		if ($backupParms[9] != "") {
			$dir_result = ftp_chdir($conn_id, $backupParms[9]);
		}
		if (ftp_put($conn_id, $filename, '/tmp/'.$filename, FTP_BINARY)) {
			webcp_log(3,0,'backup.php',"Backup: Remote Databases Successful",0,$echo);
		} else {
			webcp_log(3,0,'backup.php',"Backup: Remote Databases Failed",0,$echo);
		}
		ftp_close($conn_id);
	}
	if ($backupParms[3] == "on") {
		if (copy('/tmp/'.$filename, $cfg['basedir'].'/_backups/'.$filename)) {
			chown($cfg['basedir'].'/_backups/'.$filename, $cfg['httpd_user']);
			webcp_log(3,0,'backup.php',"Backup: Local Databases Successful",0,$echo);
		} else {
			webcp_log(3,0,'backup.php',"Backup: Local Databases Failed",0,$echo);
		}
	}
	// Cleanup - Remove compressed tmp file
	unlink('/tmp/'.$filename);

	// Cleanup sql files
	$backup_files = explode(' ', $backup_files);
	foreach($backup_files as $file) {
		unlink($file);
	}
	
	webcp_log(3,0,'backup.php',"Backup: Completed Databases",0,1);
}
?>
